package rmioverlay.client;

import java.rmi.*;
import rmioverlay.server.ServerAppInterface;
import java.net.MalformedURLException;

@SuppressWarnings("rawtypes")
public class Client<T extends ClientApp> {
	private T app;
	
	/**
	 * Instantiates a new client running the given client application
	 * and connects it to the server app expected to be running
	 * on the given hostname.
	 * @param clientApp The client application to run
	 * @param hostname The hostname to which the client connects to
	 * @param appName The application to ask to the server 
	 * @throws RemoteException if a network issue occurs
	 * @throws MalformedURLException if hostname is not a correct URL
	 * @throws NotBoundException if server is not running the required application
	 */
	public Client(T clientApp, String hostname, String appName)
			throws RemoteException, MalformedURLException, NotBoundException {
		
		//Stores the reference of given ClientApp
		this.app = clientApp;
		
		//Gets the ServerApp object from the server and stores the reference in the ClientApp
		this.app.setServer((ServerAppInterface) Naming.lookup("//"+hostname+"/"+appName));
		
		//Registers connection in the server
		this.app.connect();
	}

	/**
	 * Returns the application reference
	 * @return The application reference
	 */
	public T getClientApp() { return this.app; }
}